package com.changpei.core.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通信
 *
 * @author Way on 2021/8/25
 */
@Configuration
public class ContentMqConfig {

    @Autowired
    private RabbitInfoConfig rabbitInfoConfig;

    /**
     * 创建队列
     * @return
     */
    @Bean("ContentQueue")
    public Queue contentQueue() {
        return new Queue(rabbitInfoConfig.getQueue().get("content"));
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean("ContentExchange")
    public Exchange contentExchange() {
        return new DirectExchange(rabbitInfoConfig.getExchange().get("content"), true, false);
    }

    /**
     * 队列绑定交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding contentQueueExchange(@Qualifier("ContentQueue") Queue queue, @Qualifier("ContentExchange") Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(rabbitInfoConfig.getRouting().get("content"))
                .noargs();
    }

}
